package ru.linnenson.darshana.infrastructure.services;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import ru.linnenson.darshana.application.domain.SubjectEnum;
import ru.linnenson.darshana.application.services.DictionaryService;
import ru.linnenson.darshana.infrastructure.persistence.models.LevelStore;
import ru.linnenson.darshana.infrastructure.persistence.models.NumberInKimStore;
import ru.linnenson.darshana.infrastructure.persistence.models.SourceStore;
import ru.linnenson.darshana.infrastructure.persistence.models.ThemeStore;
import ru.linnenson.darshana.infrastructure.persistence.repositories.LevelPersistenceRepository;
import ru.linnenson.darshana.infrastructure.persistence.repositories.MathTaskPersistenceRepository;
import ru.linnenson.darshana.infrastructure.persistence.repositories.NumberInKimPersistenceRepository;
import ru.linnenson.darshana.infrastructure.persistence.repositories.SourcePersistenceRepository;
import ru.linnenson.darshana.infrastructure.persistence.repositories.ThemePersistenceRepository;
import ru.linnenson.darshana.infrastructure.rest.dto.DictionaryDto;
import ru.linnenson.darshana.infrastructure.rest.dto.TagDto;
import ru.linnenson.darshana.infrastructure.services.mappers.DictionaryDtoMapper;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class DictionaryServiceImpl implements DictionaryService {

  private final DictionaryDtoMapper dictionaryDtoMapper;
  private final LevelPersistenceRepository levelPersistenceRepository;
  private final ThemePersistenceRepository themePersistenceRepository;
  private final SourcePersistenceRepository sourcePersistenceRepository;
  private final MathTaskPersistenceRepository mathTaskPersistenceRepository;
  private final NumberInKimPersistenceRepository numberInKimPersistenceRepository;

  @Override
  public List<DictionaryDto> getSources(final SubjectEnum subject) {

    log.debug("find sources by subject {}", subject);

    final List<SourceStore> sources = sourcePersistenceRepository.findAllBySubjectSysnameOrderById(subject);

    return dictionaryDtoMapper.toSourceDto(sources);
  }

  @Override
  public List<DictionaryDto> getLevels(final SubjectEnum subject) {

    log.debug("find levels by subject {}", subject);

    final List<LevelStore> levels = levelPersistenceRepository.findAllBySubjectSysnameOrderById(subject);

    return dictionaryDtoMapper.toLevelDto(levels);
  }

  @Override
  public List<DictionaryDto> getThemes(final SubjectEnum subject) {

    log.debug("find themes by subject {}", subject);

    final List<ThemeStore> themes = themePersistenceRepository.findAllBySubjectSysnameOrderById(subject);

    return dictionaryDtoMapper.toThemeDto(themes);
  }

  @Override
  public List<TagDto> getTags(final SubjectEnum subject) {

    if (SubjectEnum.MATH.equals(subject)) {
      final List<String> tags = mathTaskPersistenceRepository.findAllTags();
      return dictionaryDtoMapper.toTagsDto(tags);
    }

    return Collections.emptyList();
  }

  @Override
  public List<DictionaryDto> getNumbersInKim(final SubjectEnum subject) {

    log.debug("find number in kim by subject {}", subject);

    final List<NumberInKimStore> numbers = numberInKimPersistenceRepository.findAllBySubjectSysnameOrderById(subject);

    return dictionaryDtoMapper.toNumberInKimDto(numbers);
  }
}
